package com.new_wear.project.system.taskdo.controller;

import java.util.List;

import com.new_wear.common.utils.security.ShiroUtils;
import com.new_wear.project.monitor.server.domain.Sys;
import com.new_wear.project.system.dept.domain.Dept;
import com.new_wear.project.system.dept.service.IDeptService;
import com.new_wear.project.system.task.domain.CstTask;
import com.new_wear.project.system.task.service.ICstTaskService;
import com.new_wear.project.system.user.domain.User;
import com.new_wear.project.system.user.service.IUserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.new_wear.framework.aspectj.lang.annotation.Log;
import com.new_wear.framework.aspectj.lang.enums.BusinessType;
import com.new_wear.project.system.taskdo.domain.CstTaskDo;
import com.new_wear.project.system.taskdo.service.ICstTaskDoService;
import com.new_wear.framework.web.controller.BaseController;
import com.new_wear.framework.web.domain.AjaxResult;
import com.new_wear.common.utils.poi.ExcelUtil;
import com.new_wear.framework.web.page.TableDataInfo;

/**
 * 系统 - 任务指派Controller
 * 
 * @author new_wear
 * @date 2020-01-09
 */
@Controller
@RequestMapping("/system/taskdo")
public class CstTaskDoController extends BaseController
{
    private String prefix = "system/taskdo";

    @Autowired
    private ICstTaskDoService cstTaskDoService;
    @Autowired
    private IDeptService deptService;
    @Autowired
    private ICstTaskService cstTaskService;
    @Autowired
    private IUserService userService;

    @RequiresPermissions("system:taskdo:view")
    @GetMapping()
    public String taskdo()
    {
        return prefix + "/taskdo";
    }

    /**
     * 查询系统 - 任务指派列表
     */
    @RequiresPermissions("system:taskdo:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(CstTaskDo cstTaskDo)
    {
        startPage();
        List<CstTaskDo> list = cstTaskDoService.selectCstTaskDoList(cstTaskDo);
        if (list != null || list.size()>0){
            for (CstTaskDo cstTask:list) {
                Integer areaId = cstTask.getAreaId();
                Dept dept = deptService.selectDeptById(areaId.longValue());//地区
                cstTask.setDeptName(dept.getDeptName());
                //任务名称，id
                Integer tid = cstTask.getTid();
                CstTask task = cstTaskService.selectCstTaskById(tid);
                cstTask.settName(task.getTitle());
            }
        }

        return getDataTable(list);
    }

    /**
     * 导出系统 - 任务指派列表
     */
    @RequiresPermissions("system:taskdo:export")
    @Log(title = "系统 - 任务指派", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(CstTaskDo cstTaskDo)
    {
        List<CstTaskDo> list = cstTaskDoService.selectCstTaskDoList(cstTaskDo);
        ExcelUtil<CstTaskDo> util = new ExcelUtil<CstTaskDo>(CstTaskDo.class);
        return util.exportExcel(list, "taskdo");
    }

    /**
     * 新增系统 - 任务指派
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存系统 - 任务指派
     */
    @RequiresPermissions("system:taskdo:add")
    @Log(title = "系统 - 任务指派", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(CstTaskDo cstTaskDo)
    {
        return toAjax(cstTaskDoService.insertCstTaskDo(cstTaskDo));
    }

    /**
     * 修改系统 - 任务指派
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Integer id, ModelMap mmap)
    {
        CstTaskDo cstTaskDo = cstTaskDoService.selectCstTaskDoById(id);
        mmap.put("cstTaskDo", cstTaskDo);
        return prefix + "/edit";
    }

    /**
     * 修改保存系统 - 任务指派
     */
    @RequiresPermissions("system:taskdo:edit")
    @Log(title = "系统 - 任务指派", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(CstTaskDo cstTaskDo)
    {
        return toAjax(cstTaskDoService.updateCstTaskDo(cstTaskDo));
    }

    /**
     * 删除系统 - 任务指派
     */
    @RequiresPermissions("system:taskdo:remove")
    @Log(title = "系统 - 任务指派", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(cstTaskDoService.deleteCstTaskDoByIds(ids));
    }


}
